import { ajaxPost } from 'api/axios'
import { mapTokenIp } from 'api/config'
import { tokenStorageKey, mapTokenStorageKey } from 'common/js/config'
import { getStorage, setStorage } from 'common/js/utils'

const actions = {
  GET_GEOSERVER_ACCESS_TOKEN: async ({ commit }) => {
    const url = mapTokenIp

    const res = await ajaxPost(url, {}, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Authorization: 'Bearer ' + getStorage(tokenStorageKey).token,
      },
    });

    if (res) {
      const token = res.content.accessToken

      commit('SET_GEOSERVER_ACCESS_TOKEN', token);

      setStorage(mapTokenStorageKey, {
        createTime: new Date().getTime(),
        token,
      });
    }
  },
}

export default actions
